home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: news.sprintlink.net!news1!news
- From: rclark@iquest.net (Robert B. Clark)
- Subject: Re: I can't print the date in the format I want.
- X-Nntp-Posting-Host: ind-004-236-169.iquest.net
- Message-ID: <3129e355.114134@news.iquest.net>
- Sender: news@iquest.net (News Admin)
- Organization: IQuest Internet, Inc.
- X-Newsreader: Forte Agent .99d/16.182
- References: <4g5nbf$8s0@newsbf02.news.aol.com>
- Date: Tue, 20 Feb 1996 15:45:41 GMT
-
- On 17 Feb 1996 18:11:43 -0500, asciizero@aol.com (ASCII zero) wrote:
-
- >I am trying to use strftime() to format the date into YY-MM-DD. The format
- >seems to be working but I am unable to actually print the formatted
- >string. It compiles just fine using gcc. What have I overlooked?
-
- You need to check your compiler and make sure that you've not disabled
- those oh-so-useful warning messages:
-
- >main()
-
- NC.
-
- On second thought, I'll comment anyway. Your function ought to return
- some sort of value, if for no other reason than to reduce the flamewars
- people tend to get into over various permutations of the main()
- declaration:
-
- int main(void)
-
- > debug = strftime(str, 50, "The current date is %y-%m-%d", ptr);
-
- "Possible use of 'str' before definition."
-
- You've got a char pointer that points to nothing in particular--you need
- to allocate space for this string either by malloc'ing it, or by
- explicitly defining a char array of sufficient size.
-
- > printf("-> %i", debug); /* can we get the length of the
- >formatted string? */
-
- Yes. Use the strlen() function (#include <string.h>).
-
- > getchar(); /* OK up to this point? */
-
- "Code has no effect."
-
- Doesn't do anything but wait for the user to press a key. If that was
- your intent, it would be better to express this as
-
- if getchar();
-
- (and possibly wrap it in a suitably-named macro) if nothing else.
-
- > printf("%s", str);
- >
- >}
-
- "Function should return a value."
-
- See my comments under main().
-
- Here is a slight revision of your code, with the disastrous null pointer
- taken care of by explicitly declaring a char array for str instead of
- just a char pointer:
-
- #include <stdio.h>
- #include <time.h>
- #include <string.h> /* For strlen() function */
- #define SLEN 50 /* Allocate 50 chars for str */
-
- int main(void)
- {
- struct tm *ptr;
- time_t lt;
- char str[SLEN]; /* Explicitly allocate 50 chars for str */
- int debug;
-
- lt = time(NULL);
- ptr = localtime(<);
- printf("\nThe system clock shows: %s\n\n",asctime(ptr));
- debug=strftime(str,SLEN,"The current date is %y-%m-%d",ptr);
- printf("\nThe value of 'debug' is %d"
- "\nThe length of 'str' is %d", debug,strlen(str));
- printf("\nFormatted time string: \"%s\"",str);
- return debug;
- }
-
- Running this program should yield the following:
-
- The system clock shows: Tue Feb 20 10:35:42 1996
-
- The value of 'debug' is 28
- The length of 'str' is 28
- Formatted time string: "The current date is 96-02-20"
- --
- Robert B. Clark <rclark@iquest.net>
- "Be wary of strong spirits. It can make you shoot at tax collectors...
- and miss." --RAH
-